<template>
  <div class="mainBox">
    <div class="login-container">
      <h1>登录</h1>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input id="username" v-model="username" type="text" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            required
          />
        </div>
        <button type="submit" class="login-button">登录</button>
      </form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/index/login.js'
import { ElNotification } from 'element-plus'
export default {
  data () {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    async handleLogin () {
      // 这里可以添加登录逻辑，例如调用 API
      const res = await login({ username: this.username, password: this.password })
      if (res.code == '200') {
        // 登录成功后的处理逻辑
        console.log('登录成功', res.data)
        // 保存用户信息到本地存储
        localStorage.setItem('user', JSON.stringify(res.data.user[0]))
        localStorage.setItem('token', res.data.access_token)
        // 跳转到首页或其他页面
        this.$router.push('/')
      } else {
        ElNotification({
          title: '登录失败',
          message: '请输入正确的账号密码',
          type: 'error',
        })
        // 登录失败后的处理逻辑
        // 可以在这里显示错误信息给用户
      }
    },
  },
};
</script>

<style scoped>
.mainBox {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-container {
  width: 500px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  background: linear-gradient(135deg, #a18cd1 0%, #f7b0e3 100%);
  color: white;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  animation: gradientBG 15s ease infinite;
  background-size: 400% 400%;
}

@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

h1 {
  text-align: center;
  margin-bottom: 20px;
  color: white;
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: white;
}

input {
  width: 450px;
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  transition: all 0.3s ease;
}

input:focus {
  outline: none;
  border-color: #fff;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.login-button:hover {
  background-color: #ff5252;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
}
</style>